package com.gimi.cloud.jimi.app.consumer.service.impl;

import com.gimi.cloud.jimi.app.consumer.feign.RegisterFeignService;
import com.gimi.cloud.jimi.app.consumer.service.RegisterService;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.NumberUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.prod.ProdCategoryDTO;
import com.gimi.cloud.jimi.dto.user.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by shazhiwei on 2017/10/17.
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    private static Logger logger = LoggerFactory.getLogger(RegisterServiceImpl.class);
    private RegisterFeignService registerFeignService;

    public RegisterServiceImpl(RegisterFeignService registerFeignService){
        this.registerFeignService = registerFeignService;
    }

    @Override
    public void doLoginByPassword(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO params = JsonUtil.jsonToBean(body,UserDTO.class);
        EntityVerification.checkByGroupsThrowsException(params,UserDTO.DoLoginByPassword.class);
        Util.sendResponse(registerFeignService.doLoginByPassword(params), httpServletResponse);
    }

    @Override
    public void doLoginByPwdCode(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        logger.info("srcData:" + body);
        UserDTO userDTO = JsonUtil.jsonToBean(body,UserDTO.class);
        EntityVerification.checkByGroupsThrowsException(userDTO,UserDTO.DoLoginByPwdCode.class);
        Util.sendResponse(registerFeignService.doLoginByPwdCode(userDTO), httpServletResponse);
    }

    @Override
    public void modifyPwd(String body, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("srcData:" + body);
        UserDTO userDTO = JsonUtil.jsonToBean(body,UserDTO.class);
        if(userDTO != null){
            String userId = httpServletRequest.getHeader("userId");
            userDTO.setUserId(NumberUtil.getInt(userId));
        }
        EntityVerification.checkByGroupsThrowsException(userDTO,UserDTO.ModifyPwd.class);
        Util.sendResponse(registerFeignService.modifyPwd(userDTO), httpServletResponse);
    }

    @Override
    public void initPwd(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        UserDTO userDTO = JsonUtil.jsonToBean(requestData,UserDTO.class);
        EntityVerification.checkByGroupsThrowsException(userDTO,UserDTO.InitPwd.class);
        Util.sendResponse(registerFeignService.initPassword(userDTO), httpServletResponse);
    }

    @Override
    public void checkModifyPassword(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        UserDTO userDTO = JsonUtil.jsonToBean(requestData,UserDTO.class);
        EntityVerification.checkByGroupsThrowsException(userDTO,UserDTO.CheckModifyPassword.class);
        Util.sendResponse(registerFeignService.checkModifypassword(userDTO), httpServletResponse);
    }

    @Override
    public void modifyPasswordByPwdCode(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest request) {
        logger.info("srcData:" + requestData);
        UserDTO userDTO = JsonUtil.jsonToBean(requestData,UserDTO.class);
        if(userDTO != null){
            String userId = request.getHeader("userId");
            userDTO.setUserId(NumberUtil.getInt(userId));
        }
        EntityVerification.checkByGroupsThrowsException(userDTO,UserDTO.ModifyPasswordByPwdCode.class);
        Util.sendResponse(registerFeignService.modifyPasswordByPwdcode(userDTO), httpServletResponse);
    }

}
